//
//  EMSAppInitializer.m
//  EVE Mac Suite
//
//  Created by Vadim Lozko on 3/20/09.
//  Copyright 2009 __Vadim__CompanyName__. All rights reserved.
//

#import "EMSAppInitializer.h"
#import "EMSAppController.h"
#import "EMSGeneralUtils.h"
#import "EVEAPIServerStatus.h"
#import "EVEAPISkillTree.h"
#import "BoolToOnlineStatusValueTransformer.h"
#import "DateToMinutesSecondsValueTransformer.h"
#import "SecondsToDateStringValueTransformer.h"
#import "SecondsToCountdownStringValueTransformer.h"

@implementation EMSAppInitializer

-(id)initWithAppController:(EMSAppController*)aController;
{
	if(self = [super init])
	{
		BoolToOnlineStatusValueTransformer* valTransformer = [[[BoolToOnlineStatusValueTransformer alloc] init] autorelease];
		DateToMinutesSecondsValueTransformer* dateToMinutesValTransformer = [[[DateToMinutesSecondsValueTransformer alloc] init] autorelease];
		SecondsToDateStringValueTransformer* valTransformer1 = [[[SecondsToDateStringValueTransformer alloc] init] autorelease];
		SecondsToCountdownStringValueTransformer* valTransformer2 = [[[SecondsToCountdownStringValueTransformer alloc] init] autorelease];
		[NSValueTransformer setValueTransformer:valTransformer forName:@"BoolToOnlineStatusValueTransformer"];	
		[NSValueTransformer setValueTransformer:valTransformer1 forName:@"SecondsToDateStringValueTransformer"];
		[NSValueTransformer setValueTransformer:valTransformer2 forName:@"SecondsToCountdownStringValueTransformer"];
		[NSValueTransformer setValueTransformer:dateToMinutesValTransformer forName:@"DateToMinutesSecondsValueTransformer"];
		appController = aController;
		NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
		[defaultCenter addObserver:self 
						  selector:@selector(skillTreeUpdateDidFinish:) 
							  name:SKILL_TREE_API_TYPE 
							object:nil];
		[defaultCenter addObserver:self 
						  selector:@selector(characterSheetUpdateDidFinish:) 
							  name:CHARACTER_SHEET_API_TYPE 
							object:nil];
		numberOfCharactersCounter = 0;
		NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
		if(![defaults stringForKey:@"selectedTimeZone"])
		{
			[defaults setObject:@"Local Time" forKey:@"selectedTimeZone"];
			[defaults setObject:@"mm/dd/yyyy" forKey:@"selectedDateFormat"];
		}
	}
	return self;
}

-(void)start
{
	[[EVEAPIServerStatus serverStatus] setDelegate:self];
	[[EVEAPIServerStatus serverStatus] update];
	[[EVEAPISkillTree apiSkillTree] setDelegate:self];
	[[EVEAPISkillTree apiSkillTree] performSelector:@selector(update) withObject:nil afterDelay:.1];
}

-(void)skillTreeUpdateDidFinish:(NSNotification*)notification
{	
	BOOL success = [[[notification userInfo] objectForKey:NOTIFICATION_SUCCESS_UPDATE_KEY] boolValue];
	if(success)
	{
		NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
		if([defaults arrayForKey:USER_DEFAULTS_CHARACTERS_KEY] && 
		   (numberOfCharacters = [[defaults arrayForKey:USER_DEFAULTS_CHARACTERS_KEY] count]) > 0)
		{

			NSArray* defaultsCharactersArray = [[defaults arrayForKey:USER_DEFAULTS_CHARACTERS_KEY] copy];
			for(NSDictionary *characterInfo in defaultsCharactersArray)
			{
				[appController createNewCharacterFromInfo:characterInfo];
			}
			[defaultsCharactersArray release];
		} else {
			[[NSNotificationCenter defaultCenter] postNotificationName:DID_UPDATE_ALL_CHARACTERS_NOTIFICATION_NAME object:self];
			[appController createNewCharacter:nil];
		}
	}
}

-(void)characterSheetUpdateDidFinish:(NSNotification*)notification
{
	numberOfCharactersCounter++;
	if(numberOfCharactersCounter ==  numberOfCharacters)
	{
		[[NSNotificationCenter defaultCenter] postNotificationName:DID_UPDATE_ALL_CHARACTERS_NOTIFICATION_NAME object:self];
	}
}

@end



